package ru.onlinestore.mobile.core

sealed class Response <out T>(open val state: ResponseState) {

    fun <R> map(mapper: ((T) -> R)): Response<R> =
        when(this) {
            is Success -> Success(data = mapper(data))

            is Loading -> Loading()

            is Error -> Error(exception = exception)

            is Empty -> Empty()
        }

    class Success <T>(val data: T): Response<T>(state = ResponseState.SUCCESS)

    class Loading: Response<Nothing>(state = ResponseState.LOADING)

    class Error(val exception: Exception): Response<Nothing>(state = ResponseState.ERROR)

    class Empty: Response<Nothing>(state = ResponseState.EMPTY)
}